package com.tojabo.piano;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.tojabo.util.SystemUtil;

public class MainFrame {

	private JFrame frame;
	private PianoPanel pianoPanel;
	
	static final int WIDTH = 1280;
	static final int HEIGHT = 768;
	
	public MainFrame() {
		frame = new JFrame();
		frame.setSize(WIDTH, HEIGHT);
		frame.setTitle("钢琴");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		try {			
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		pianoPanel = new PianoPanel();
		
		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		toolkit.addAWTEventListener( new MyKeyBoardListener(), AWTEvent.KEY_EVENT_MASK);
		
		frame.add(pianoPanel);
		
		SystemUtil.showInCenter(frame);
		//frame.setUndecorated(true);  
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new MainFrame();
			}
		});
	}
	

	
	class MyKeyBoardListener implements AWTEventListener {
		public void eventDispatched(AWTEvent e) {
			if (e.getID() == KeyEvent.KEY_PRESSED) {
				KeyEvent evt = (KeyEvent) e;
				if (evt.getKeyCode() == KeyEvent.VK_Z) {
					pianoPanel.changeButImg(0,1);
					SystemUtil.play_Action(0);
				}else if(evt.getKeyCode() == KeyEvent.VK_X){
					pianoPanel.changeButImg(1,1);
					SystemUtil.play_Action(1);
				}else if(evt.getKeyCode() == KeyEvent.VK_C){
					pianoPanel.changeButImg(2,1);
					SystemUtil.play_Action(2);
				}else if(evt.getKeyCode() == KeyEvent.VK_V){
					pianoPanel.changeButImg(3,1);
					SystemUtil.play_Action(3);
				}else if(evt.getKeyCode() == KeyEvent.VK_B){
					pianoPanel.changeButImg(4,1);
					SystemUtil.play_Action(4);
				}else if(evt.getKeyCode() == KeyEvent.VK_N){
					pianoPanel.changeButImg(5,1);
					SystemUtil.play_Action(5);
				}else if(evt.getKeyCode() == KeyEvent.VK_M){
					pianoPanel.changeButImg(6,1);
					SystemUtil.play_Action(6);
				}
				
				else if(evt.getKeyCode() == KeyEvent.VK_S){
					pianoPanel.changeButImg(7,1);
					SystemUtil.play_Action(7);
				}else if(evt.getKeyCode() == KeyEvent.VK_D){
					pianoPanel.changeButImg(8,1);
					SystemUtil.play_Action(8);
				}else if(evt.getKeyCode() == KeyEvent.VK_F){
					pianoPanel.changeButImg(9,1);
					SystemUtil.play_Action(9);
				}else if(evt.getKeyCode() == KeyEvent.VK_G){
					pianoPanel.changeButImg(10,1);
					SystemUtil.play_Action(10);
				}else if(evt.getKeyCode() == KeyEvent.VK_H){
					pianoPanel.changeButImg(11,1);
					SystemUtil.play_Action(11);
				}else if(evt.getKeyCode() == KeyEvent.VK_J){
					pianoPanel.changeButImg(12,1);
					SystemUtil.play_Action(12);
				}else if(evt.getKeyCode() == KeyEvent.VK_K){
					pianoPanel.changeButImg(13,1);
					SystemUtil.play_Action(13);
				}
				
				else if(evt.getKeyCode() == KeyEvent.VK_W){
					pianoPanel.changeButImg(14,1);
					SystemUtil.play_Action(14);
				}else if(evt.getKeyCode() == KeyEvent.VK_E){
					pianoPanel.changeButImg(15,1);
					SystemUtil.play_Action(15);
				}else if(evt.getKeyCode() == KeyEvent.VK_R){
					pianoPanel.changeButImg(16,1);
					SystemUtil.play_Action(16);
				}else if(evt.getKeyCode() == KeyEvent.VK_T){
					pianoPanel.changeButImg(17,1);
					SystemUtil.play_Action(17);
				}else if(evt.getKeyCode() == KeyEvent.VK_Y){
					pianoPanel.changeButImg(18,1);
					SystemUtil.play_Action(18);
				}else if(evt.getKeyCode() == KeyEvent.VK_U){
					pianoPanel.changeButImg(19,1);
					SystemUtil.play_Action(19);
				}else if(evt.getKeyCode() == KeyEvent.VK_I){
					pianoPanel.changeButImg(20,1);
					SystemUtil.play_Action(20);					
				}
				
			}// end KeyEvent.KEY_PRESSED
			
			
			if (e.getID() == KeyEvent.KEY_RELEASED) {
				KeyEvent evt = (KeyEvent) e;
				if(evt.getKeyCode() == KeyEvent.VK_SPACE){				
					pianoPanel.changeButImg(1,0);
				}
				
				if (evt.getKeyCode() == KeyEvent.VK_Z) {
					pianoPanel.changeButImg(0,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_X){
					pianoPanel.changeButImg(1,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_C){
					pianoPanel.changeButImg(2,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_V){
					pianoPanel.changeButImg(3,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_B){
					pianoPanel.changeButImg(4,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_N){					
					pianoPanel.changeButImg(5,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_M){					
					pianoPanel.changeButImg(6,0);
				}
				
				else if(evt.getKeyCode() == KeyEvent.VK_S){
					pianoPanel.changeButImg(7,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_D){
					pianoPanel.changeButImg(8,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_F){
					pianoPanel.changeButImg(9,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_G){
					pianoPanel.changeButImg(10,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_H){
					pianoPanel.changeButImg(11,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_J){
					pianoPanel.changeButImg(12,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_K){
					pianoPanel.changeButImg(13,0);
				}
				
				else if(evt.getKeyCode() == KeyEvent.VK_W){
					pianoPanel.changeButImg(14,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_E){
					pianoPanel.changeButImg(15,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_R){
					pianoPanel.changeButImg(16,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_T){
					pianoPanel.changeButImg(17,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_Y){
					pianoPanel.changeButImg(18,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_U){
					pianoPanel.changeButImg(19,0);
				}else if(evt.getKeyCode() == KeyEvent.VK_I){
					pianoPanel.changeButImg(20,0);
				}
			} //end KeyEvent.KEY_RELEASED
			
		}
		
	}

}
